package com.db.collections;

import java.lang.management.ManagementFactory;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;
import java.util.TreeMap;

public class MemoryConsumptionText {
	static final int MAX = Integer.MAX_VALUE;

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		int n = 100000;
		Random rand = new Random();
		int[] array;

		long mem1, mem;
		System.gc();
		mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		array = new int[n];
		for (int i=0; i<n; i++) {
			array[i] = i;
		}
		System.gc();
		mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		System.out.printf("array: %d%n", mem1 - mem);
		int sum = 0;
		for (int i=0; i<n; i++)
			sum += array[i];
		System.out.println(sum);
		
		System.gc();
		HashMap<Integer, Integer> hashMap;
		System.gc();
		mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		hashMap = new HashMap<>();
		for (int i=0; i<n; i++)
			hashMap.put(i, i);
		System.gc();
		mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		System.out.printf("hashMap: %d%n", mem1 - mem);
		System.out.println(hashMap.size());

		System.gc();
		mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		TreeMap<Integer, Integer> treeMap = new TreeMap<>();
		for (int i=0; i<n; i++)
			treeMap.put(i, rand.nextInt(MAX));
		System.gc();
		mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		System.out.printf("treemap: %d%n", mem1 - mem);
		System.out.println(treeMap.size());

		System.gc();
		mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		ArrayList<Integer> arrayList = new ArrayList<>();
		for (int i=0; i<n; i++) 
			arrayList.add(rand.nextInt(MAX));
		System.gc();
		mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		System.out.printf("arrayList: %d%n",  mem1 - mem);
		System.out.println(arrayList.size());

		System.gc();
		mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		LinkedList<Integer> linkedList = new LinkedList<>();
		for (int i=0; i<n; i++)
			linkedList.add(rand.nextInt(MAX));
		System.gc();
		mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		System.out.printf("linkedList: %d%n", mem1 - mem);
		System.out.println(linkedList.size());
	}
}
